package com.da.javatest.leetCode;

import java.util.HashMap;
import java.util.Map;

/**
 * @author chenlida
 * @date 2019/1/9 19:54
 * @description
 */
public class Code451 {
    public static void main(String[] args) {
        new Code451().frequencySort("tree");
    }

    public String frequencySort(String s) {
        StringBuilder sb = new StringBuilder();
        Map<Character, Integer> map = new HashMap<>(s.length());
        for (char c : s.toCharArray()) {
            if (map.containsKey(c)) {
                map.put(c, map.get(c) + 1);
            } else {
                map.put(c, 1);
            }
        }
        map.entrySet().stream().sorted((x, y) -> y.getValue() - x.getValue()).forEach(e -> {
            for (Integer i = 0; i < e.getValue(); i++) {
                sb.append(e.getKey());
            }
        });
        return sb.toString();
    }
}
